﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Common.Admin;
using System.Linq;

using System.Collections.Generic;

namespace Common
{    
    public class StudyDataClient
    {
        #region Private Variables
        private StudyManager _studyManager = StudyManager.GetInstance();        
        #endregion

        #region Constructor
        public StudyDataClient()
        {

        }
        #endregion

        #region Select methods
        public delegate void LoadDataCompletedDelegate();
        public event LoadDataCompletedDelegate LoadDataCompleted;
        /// <summary>
        /// This is run when a user logs into the participant application
        /// </summary>
        public void LoadData()
        {
            AdminServiceWCFClient a = StudyManager.GetAdminServiceWCFClient();            
            a.GetSurveyDataCompleted += new EventHandler<GetSurveyDataCompletedEventArgs>(delegate(object sender, GetSurveyDataCompletedEventArgs e)
            {
                _studyManager.Studies = e.Result.ToList();
                _studyManager.Conditions = e.Conditions.ToList();
                _studyManager.StudyWaves = e.Waves.ToList();
                _studyManager.StudyWaveSurveys = e.StudyWaveSurveys.ToList();
                _studyManager.Surveys = e.Surveys.ToList();
                _studyManager.SurveySurveyPages = e.SurveySurveyPages.ToList();
                _studyManager.SurveyPages = e.Pages.ToList();
                _studyManager.StudyClaims = e.StudyClaims.ToList();
                AdminServiceWCFClient b = StudyManager.GetAdminServiceWCFClient();
                b.GetClaimsCompleted += new EventHandler<GetClaimsCompletedEventArgs>(delegate(object senderclaim, GetClaimsCompletedEventArgs eclaim)
                {
                    _studyManager.Claims = eclaim.Result.ToList();
                    //We make another call to load all surveys in the database
                    //not just ones associated with a study
                    AdminServiceWCFClient c = StudyManager.GetAdminServiceWCFClient();
                    c.GetAllSurveysCompleted += new EventHandler<GetAllSurveysCompletedEventArgs>(delegate(object sender2, GetAllSurveysCompletedEventArgs e2)
                    {
                        foreach (Survey s in e2.Result)
                        {
                            if (GetSurvey(s.SurveyId) == null)
                            {
                                _studyManager.Surveys.Add(s);
                            }
                        }
                        LoadDataCompleted();
                    });
                    c.GetAllSurveysAsync();                    
                });
                b.GetClaimsAsync();
                b.CloseAsync();
            });
            a.GetSurveyDataAsync();
            a.CloseAsync();
        }
        /// <summary>
        /// This is run when the admin logs into the admin Silverlight application
        /// </summary>
        /// <param name="AdminUsernameId"></param>
        public void LoadDataForAdmin(int AdminUsernameId)
        {
            AdminServiceWCFClient a = StudyManager.GetAdminServiceWCFClient();
            a.GetSurveyDataCompleted += new EventHandler<GetSurveyDataCompletedEventArgs>(delegate(object sender, GetSurveyDataCompletedEventArgs e)
            {
                _studyManager.Studies = e.Result.ToList();
                _studyManager.Conditions = e.Conditions.ToList();
                _studyManager.StudyWaves = e.Waves.ToList();
                _studyManager.StudyWaveSurveys = e.StudyWaveSurveys.ToList();
                _studyManager.Surveys = e.Surveys.ToList();
                _studyManager.SurveySurveyPages = e.SurveySurveyPages.ToList();
                _studyManager.SurveyPages = e.Pages.ToList();
                //We make another call to load all surveys in the database
                //not just ones associated with a study
                AdminServiceWCFClient b = StudyManager.GetAdminServiceWCFClient();
                b.GetAllSurveysCompleted += new EventHandler<GetAllSurveysCompletedEventArgs>(delegate(object sender2, GetAllSurveysCompletedEventArgs e2)
                {
                    foreach (Survey s in e2.Result)
                    {
                        if (_studyManager.Surveys.Contains(s) == false)
                        {
                            _studyManager.Surveys.Add(s);
                        }
                    }                
                });
                b.GetAllSurveysAsync();
            });
            a.GetSurveyDataAsync();
            a.CloseAsync();
        }

        void b_GetAllSurveysCompleted(object sender, GetAllSurveysCompletedEventArgs e)
        {
            throw new NotImplementedException();
        }
        public List<Condition> GetConditions(Guid StudyId)
        {
            return (from x in _studyManager.Conditions
                    where x.StudyId == StudyId
                    select x).ToList();
        }
        public List<StudyWave> GetStudyWaves(Guid StudyId)
        {
            return (from x in _studyManager.StudyWaves
                    where x.StudyId == StudyId
                    select x).ToList();
        }
        public List<StudyWaveSurvey> GetStudyWaveSurveys(int StudyWaveId, int ConditionId)
        {
            return (from x in _studyManager.StudyWaveSurveys
                    where x.StudyWaveId == StudyWaveId
                    && x.Conditionid == ConditionId
                    orderby x.DisplayOrder
                    select x).ToList();
        }
        public StudyWaveSurvey GetStudyWaveSurveys(int StudyWaveId, int ConditionId, Guid SurveyId)
        {
            return (from x in _studyManager.StudyWaveSurveys
                    where x.StudyWaveId == StudyWaveId
                    && x.Conditionid == ConditionId
                    && x.SurveyId == SurveyId
                    orderby x.DisplayOrder
                    select x).First();
        }
        public List<Survey> GetSurveys(int StudyWaveId, int ConditionId)
        {
            return
             (from y in _studyManager.Surveys
              join x in _studyManager.StudyWaveSurveys on y.SurveyId equals x.SurveyId
              where x.StudyWaveId == StudyWaveId
              && x.Conditionid == ConditionId
              orderby x.DisplayOrder
              select y).ToList();
        }
        public Survey GetSurvey(Guid SurveyId)
        {
            return (from y in _studyManager.Surveys
                    where y.SurveyId == SurveyId
                    select y).SingleOrDefault();
        }        
        public List<SurveySurveyPage> GetSurveySurveyPages(Guid SurveyId)
        {
            return (from x in _studyManager.SurveySurveyPages
                    where x.SurveyId == SurveyId
                    orderby x.DisplayOrder
                    select x).ToList();
        }
        public List<SurveyPage> GetSurveyPages(Guid SurveyId)
        {
            return
                (from y in _studyManager.SurveyPages
                 join x in _studyManager.SurveySurveyPages on y.SurveyPageId equals x.SurveyPageId
                 where x.SurveyId == SurveyId
                 orderby x.DisplayOrder
                 select y).ToList();
        }
        public SurveyPage GetSurveyPage(int SurveyPageId)
        {
            return (from y in _studyManager.SurveyPages
                    where y.SurveyPageId == SurveyPageId
                    select y).SingleOrDefault();
        }
        public void LogUserIntoStudy(int IndividualId, Guid StudyId, bool IsTesting)
        {
            //Log the user into the session
            AdminServiceWCFClient a = StudyManager.GetAdminServiceWCFClient();
            a.BeginSessionAsync(IndividualId, StudyId, IsTesting);
            a.CloseAsync();
        }
        public delegate void UserAccountCreatedDelegate(bool Successful);
        public event UserAccountCreatedDelegate UserAccountCreated;
        public void CreateUserAccount(string Username, string Password)
        {
            AdminServiceWCFClient a = StudyManager.GetAdminServiceWCFClient();
            a.CreateAccountCompleted += new EventHandler<CreateAccountCompletedEventArgs>(delegate(object sender, CreateAccountCompletedEventArgs e)
            {
                UserAccountCreated(e.Result);
            });
            a.CreateAccountAsync(Username, Password);
        }
        public delegate void ResumeStudyDelegate(Condition myCondition
            , Study myStudy, DateTime? FutureStartDate
            , DateTime? FutureEndDate
            , List<StudyConsolidatedOrderedIndividualResult> SurveyPages);
        public event ResumeStudyDelegate ResumeStudyEvent;
        public void ResumeStudy(int IndividualId, Guid StudyId, bool IsTesting)
        {
            AdminServiceWCFClient a = StudyManager.GetAdminServiceWCFClient();
            a.ResumeStudyCompleted += new EventHandler<ResumeStudyCompletedEventArgs>(delegate(object sender1, ResumeStudyCompletedEventArgs e1)
            {
                ResumeStudyEvent(e1.myCondition, e1.Result, e1.NextWaveStartDate, e1.NextWaveEndDate, e1.SurveyPages.ToList());
            });
            a.ResumeStudyAsync(IndividualId, StudyId, IsTesting);
        }
        public delegate void GetClaimsForStudyDelegate(List<Claim> Claims);
        public event GetClaimsForStudyDelegate GetClaimsForStudyEvent;
        public void GetClaimsForStudy(Guid StudyId)
        {
            AdminServiceWCFClient a = StudyManager.GetAdminServiceWCFClient();
            a.GetClaimsAddedCompleted += new EventHandler<GetClaimsAddedCompletedEventArgs>(delegate(object sender, GetClaimsAddedCompletedEventArgs e)
            {
                GetClaimsForStudyEvent(e.Result);
            });
            a.GetClaimsAddedAsync(StudyId);
        }
        public delegate void GetClaimsForIndividualDelegate(List<IndividualClaimsView> Claims);
        public event GetClaimsForIndividualDelegate GetClaimsForIndividualEvent;
        public void GetClaimsForIndividual(string LoginName, List<int> ClaimIds)
        {
            AdminServiceWCFClient a = StudyManager.GetAdminServiceWCFClient();
            a.GetClaimsForIndividualCompleted += new EventHandler<GetClaimsForIndividualCompletedEventArgs>(delegate(object sender, GetClaimsForIndividualCompletedEventArgs e)
            {
                GetClaimsForIndividualEvent(e.Result);
            });
            a.GetClaimsForIndividualAsync(LoginName, ClaimIds);
        }
        public void GetClaims(Guid StudyId, out List<Claim> ClaimsAdded, out List<Claim> ClaimsNotAdded)
        {
            ClaimsAdded = new List<Claim>();
            ClaimsNotAdded = new List<Claim>();
            ClaimsNotAdded.AddRange(_studyManager.Claims);
            List<StudyClaim> SelectedStudyClaims = (from y in _studyManager.StudyClaims
                                                    where y.StudyId == StudyId
                                                    select y).ToList();
            foreach (StudyClaim sc in SelectedStudyClaims)
            {
                Claim myClaim = (from y in ClaimsNotAdded
                                 where y.ClaimId == sc.ClaimId
                                 select y).FirstOrDefault();
                if (myClaim != null)
                {
                    ClaimsNotAdded.Remove(myClaim);
                    ClaimsAdded.Add(myClaim);
                }
            }
        }        
        public delegate void StudyDataLoadedDelegate(List<StudyResultsIndividualEmail> Results);
        public event StudyDataLoadedDelegate StudyDataLoadedEvent;
        public void GetStudyData(Guid StudyId)
        {
            AdminServiceWCFClient a = StudyManager.GetAdminServiceWCFClient();
            a.GetStudyResultsCompleted += new EventHandler<GetStudyResultsCompletedEventArgs>(delegate(object sender, GetStudyResultsCompletedEventArgs e)
            {
                if (StudyDataLoadedEvent != null)
                {
                    StudyDataLoadedEvent(e.Result.ToList());
                }
            });
            a.GetStudyResultsAsync(StudyId);
        }
        public delegate void AllSurveysLoadedDelegate();
        public event AllSurveysLoadedDelegate AllSurveysLoaded;
        public void GetAllSurveys()
        {
            AdminServiceWCFClient a = StudyManager.GetAdminServiceWCFClient();           
            a.GetAllSurveysCompleted += new EventHandler<GetAllSurveysCompletedEventArgs>(delegate(object sender, GetAllSurveysCompletedEventArgs e)
            {
                foreach (Survey s in e.Result)
                {
                    if (_studyManager.Surveys.Contains(s) == false)
                    {
                        _studyManager.Surveys.Add(s);
                    }
                }
                if (AllSurveysLoaded != null)
                    AllSurveysLoaded();
            });
            a.GetAllSurveysAsync();
        
        }

        void a_GetAllSurveysCompleted(object sender, GetAllSurveysCompletedEventArgs e)
        {
            throw new NotImplementedException();
        }
        #endregion

        #region Updating Data
        public void UpdateClaimsForStudy(Guid StudyId, List<Claim> ClaimsAdded)
        {
            //Remove all existing claims for the study
            List<StudyClaim> ExistingClaims = (from x in _studyManager.StudyClaims
                                               where x.StudyId == StudyId
                                               select x).ToList();
            foreach (StudyClaim cs in ExistingClaims)
            {
                _studyManager.StudyClaims.Remove(cs);
            }
            //Recreate them based on supplied list
            foreach (Claim newClaim in ClaimsAdded)
            {
                StudyClaim sc = new StudyClaim();
                sc.StudyId = StudyId;
                sc.ClaimId = newClaim.ClaimId;
                _studyManager.StudyClaims.Add(sc);
            }
            //Update the database
            AdminServiceWCFClient a = StudyManager.GetAdminServiceWCFClient();
            a.UpdateClaimsForStudyAsync(StudyId, ClaimsAdded);
        }
        public void UpdateStudy(Study MyStudy)
        {
            AdminServiceWCFClient a = StudyManager.GetAdminServiceWCFClient();
            a.UpdateStudyAsync(MyStudy);
            a.CloseAsync();
        }
        public void UpdateCondition(Condition myCondition)
        {
            AdminServiceWCFClient a = StudyManager.GetAdminServiceWCFClient();
            a.UpdateConditionAsync(myCondition);
            a.CloseAsync();
        }
        public void UpdateStudyWave(StudyWave myStudyWave)
        {
            AdminServiceWCFClient a = StudyManager.GetAdminServiceWCFClient();
            a.UpdateStudyWaveAsync(myStudyWave);
            a.CloseAsync();
        }
        public void UpdateStudyWaveSurveys(int StudyWaveId, int ConditionId, List<Survey> Surveys)
        {
            //Remove all items from the database
            AdminServiceWCFClient a = StudyManager.GetAdminServiceWCFClient();
            a.DeleteStudyWaveSurveyAsync(StudyWaveId, ConditionId);
            //Remove all of them from local storage
            List<StudyWaveSurvey> CurrentList = GetStudyWaveSurveys(
                StudyWaveId, ConditionId);
            foreach (StudyWaveSurvey sws in CurrentList)
            {
                _studyManager.StudyWaveSurveys.Remove(sws);
            }
            //Add newly ordered items to database
            int DisplayOrder = 0;
            List<StudyWaveSurvey> swsListNew = new List<StudyWaveSurvey>();
            foreach (Survey sws in Surveys)
            {
                StudyWaveSurvey ssw_new = new StudyWaveSurvey();
                ssw_new.SurveyId = sws.SurveyId;
                ssw_new.StudyWaveId = StudyWaveId;
                ssw_new.Conditionid = ConditionId;
                ssw_new.DisplayOrder = DisplayOrder;
                DisplayOrder++;
                AdminServiceWCFClient b = StudyManager.GetAdminServiceWCFClient();
                b.CreateStudyWaveSurveyAsync(ssw_new);
                swsListNew.Add(ssw_new);
                b.CloseAsync();
            }
            //Add newsly ordered items to local storage 
            _studyManager.StudyWaveSurveys.AddRange(swsListNew);
            a.CloseAsync();
        }
        public void UpdateSurvey(Survey mySurvey)
        {
            AdminServiceWCFClient a = StudyManager.GetAdminServiceWCFClient();
            a.UpdateSurveyAsync(mySurvey);
            a.CloseAsync();
        }
        public void UpdateSurveyPage(SurveyPage mySp)
        {
            AdminServiceWCFClient a = StudyManager.GetAdminServiceWCFClient();
            a.UpdateSurveyPageAsync(mySp);
            a.CloseAsync();
        }
        public void UpdateSurveyPages(List<SurveyPage> Pages, Guid SurveyId)
        {
            //Remove all of them from the database
            AdminServiceWCFClient a = StudyManager.GetAdminServiceWCFClient();
            a.DeleteSurveySurveyPagesAsync(SurveyId);
            //Remove all of them from local storage
            List<SurveySurveyPage> CurrentList =  GetSurveySurveyPages(SurveyId);
            foreach (SurveySurveyPage ssp in CurrentList)
            {
                _studyManager.SurveySurveyPages.Remove(ssp);
            }
            //Add newly ordered items to database
            int DisplayOrder = 0;
            List<SurveySurveyPage> ssplistnew = new List<SurveySurveyPage>();
            foreach (SurveyPage page in Pages)
            {
                SurveySurveyPage ssp_new = new SurveySurveyPage();
                ssp_new.SurveyId = SurveyId;
                ssp_new.SurveyPageId = page.SurveyPageId;
                ssp_new.DisplayOrder = DisplayOrder;
                DisplayOrder++;
                AdminServiceWCFClient b = StudyManager.GetAdminServiceWCFClient();
                b.CreateSurveySurveyPageAsync(ssp_new);
                ssplistnew.Add(ssp_new);
                b.CloseAsync();
            }
            //Add newsly ordered items to local storage 
            _studyManager.SurveySurveyPages.AddRange(ssplistnew);
            a.CloseAsync();
        }
        public void ReportSurveyPageResults(Guid StudyId, int IndividualId
            , int StudyWaveId, Guid SurveyId, int SurveyPageId, List<string> Names, List<string> Values)
        {
            AdminServiceWCFClient a = StudyManager.GetAdminServiceWCFClient();
            a.ReportSurveyPageResultsAsync(StudyId, IndividualId
                , StudyWaveId, SurveyId
                , SurveyPageId, Names, Values);
        }
        #endregion

        #region CreateData
        public void CreateClaim(string UserName, int ClaimId, string Value)
        {
            AdminServiceWCFClient a = StudyManager.GetAdminServiceWCFClient();
            a.CreateClaimAsync(UserName, ClaimId, Value);
        }
        public delegate void CreateStudyCompletedDelegate(Study Study);
        public event CreateStudyCompletedDelegate CreateStudyCompleted;
        public void CreateStudy()
        {
            AdminServiceWCFClient a = StudyManager.GetAdminServiceWCFClient();
            a.CreateStudyCompleted += new EventHandler<CreateStudyCompletedEventArgs>(delegate(object sender, CreateStudyCompletedEventArgs e)
            {
                Study _study = e.Result;
                _studyManager.Studies.Add(_study);
                CreateStudyCompleted(_study);
            });
            a.CreateStudyAsync();
            a.CloseAsync();
        }
        public delegate void CreateWaveCompletedDelegate(StudyWave StudyWave);
        public event CreateWaveCompletedDelegate CreateWaveCompleted;
        public void CreateWave(Guid StudyId)
        {
            AdminServiceWCFClient a = StudyManager.GetAdminServiceWCFClient();
            a.CreateWaveCompleted += new EventHandler<CreateWaveCompletedEventArgs>(delegate(object sender, CreateWaveCompletedEventArgs e)
            {
                StudyWave NewWave = e.Result;
                _studyManager.StudyWaves.Add(NewWave);
                CreateWaveCompleted(NewWave);
            });
            a.CreateWaveAsync(StudyId);
            a.CloseAsync();
        }
        public delegate void CreateSurveyCompletedDelegate(Survey Survey);
        public event CreateSurveyCompletedDelegate CreateSurveyCompleted;
        public void CreateSurvey(int StudyWaveId, int ConditionId)
        {
            AdminServiceWCFClient a = StudyManager.GetAdminServiceWCFClient();
            a.CreateSurveyCompleted += new EventHandler<CreateSurveyCompletedEventArgs>(delegate(object sender, CreateSurveyCompletedEventArgs e)
            {
                Survey newSurvey = e.Result;
                StudyWaveSurvey newsws = e.StudyWaveSurvey;
                _studyManager.Surveys.Add(newSurvey);
                _studyManager.StudyWaveSurveys.Add(newsws);
                CreateSurveyCompleted(newSurvey);
            });
            a.CreateSurveyAsync(StudyWaveId, ConditionId);
            a.CloseAsync();
        }
        public delegate void AssociateSurveyCompletedDelegate(StudyWaveSurvey sws, Survey NewSurvey);
        public event AssociateSurveyCompletedDelegate AssociateSurveyCompleted;
        public void AssociateSurvey(int StudyWaveId, int ConditionId, Survey Survey)
        {
            AdminServiceWCFClient a = StudyManager.GetAdminServiceWCFClient();
            a.AssociateExistingSurveyCompleted += new EventHandler<AssociateExistingSurveyCompletedEventArgs>(delegate(object sender, AssociateExistingSurveyCompletedEventArgs e)
            {
                StudyWaveSurvey newsws = e.Result;
                _studyManager.StudyWaveSurveys.Add(newsws);
                AssociateSurveyCompleted(newsws, Survey);
            });
            a.AssociateExistingSurveyAsync(StudyWaveId, ConditionId, Survey.SurveyId);
            a.CloseAsync();
        }
        void a_AssociateExistingSurveyCompleted(object sender, AssociateExistingSurveyCompletedEventArgs e)
        {
            throw new NotImplementedException();
        }
        void a_CreateSurveyCompleted(object sender, CreateSurveyCompletedEventArgs e)
        {
            throw new NotImplementedException();
        }
        public delegate void CreateConditionCompletedDelegate(Condition Condition);
        public event CreateConditionCompletedDelegate CreateConditionCompleted;
        public void CreateCondition(Guid StudyId)
        {
            AdminServiceWCFClient a = StudyManager.GetAdminServiceWCFClient();
            a.CreateConditionCompleted += new EventHandler<CreateConditionCompletedEventArgs>(delegate(object sender, CreateConditionCompletedEventArgs e)
            {
                Condition NewCondition = e.Result;
                _studyManager.Conditions.Add(NewCondition);
                CreateConditionCompleted(NewCondition);
            });
            a.CreateConditionAsync(StudyId);
            a.CloseAsync();
        }
        public delegate void CreateSurveyPageCompletedDelegate(SurveyPage newPage);
        public event CreateSurveyPageCompletedDelegate CreateSurveyPageCompleted;
        public void CreateSurveyPage(Guid SurveyId)
        {
            AdminServiceWCFClient a = StudyManager.GetAdminServiceWCFClient();
            a.CreateSurveyPageCompleted += new EventHandler<CreateSurveyPageCompletedEventArgs>(delegate(object sender, CreateSurveyPageCompletedEventArgs e)
            {
                SurveyPage newPage = e.Result;
                _studyManager.SurveyPages.Add(newPage);
                _studyManager.SurveySurveyPages.Add(e.ssp);
                CreateSurveyPageCompleted(newPage);

            });
            a.CreateSurveyPageAsync(SurveyId);
            a.CloseAsync();
        }
        #endregion

        #region DeleteData
        public delegate void DeleteStudyCompletedDelegate(Study Study);
        public event DeleteStudyCompletedDelegate DeleteStudyCompleted;
        public void DeleteStudy(Study myStudy)
        {
            AdminServiceWCFClient a = StudyManager.GetAdminServiceWCFClient();
            a.DeleteStudyCompleted += new EventHandler<DeleteStudyCompletedEventArgs>(delegate(object sender, DeleteStudyCompletedEventArgs e)
            {
                DeleteStudyCompleted(myStudy);
            });
            a.DeleteStudyAsync(myStudy.StudyId);
            a.CloseAsync();
        }
        public delegate void DeleteConditionCompletedDelegate(Condition Condition);
        public event DeleteConditionCompletedDelegate DeleteConditionCompleted;
        public void DeleteCondition(Condition ConditionToDelete)
        {
            AdminServiceWCFClient a = StudyManager.GetAdminServiceWCFClient();
            a.DeleteConditionCompleted += new EventHandler<DeleteConditionCompletedEventArgs>(delegate(object sender, DeleteConditionCompletedEventArgs e)
            {
                _studyManager.Conditions.Remove(ConditionToDelete);
                DeleteConditionCompleted(ConditionToDelete);
            });
            a.DeleteConditionAsync(ConditionToDelete.ConditionId);
            a.CloseAsync();
        }
        public delegate void DeleteStudyWaveCompletedDelegate(StudyWave StudyWave);
        public event DeleteStudyWaveCompletedDelegate DeleteStudyWaveCompleted;
        public void DeleteStudyWave(StudyWave WavetoDelete)
        {
            AdminServiceWCFClient a = StudyManager.GetAdminServiceWCFClient();
            a.DeleteStudyWaveCompleted += new EventHandler<DeleteStudyWaveCompletedEventArgs>(delegate(object sender, DeleteStudyWaveCompletedEventArgs e)
            {
                _studyManager.StudyWaves.Remove(WavetoDelete);
                DeleteStudyWaveCompleted(WavetoDelete);
            });
            a.DeleteStudyWaveAsync(WavetoDelete.StudyWaveId);
            a.CloseAsync();
        }
        public delegate void DeleteSurveyPageCompletedDelegate(SurveyPage DeletedPage);
        public event DeleteSurveyPageCompletedDelegate DeleteSurveyPageCompleted;
        public void DeleteSurveyPage(SurveyPage SurveyPageToDelete)
        {
            AdminServiceWCFClient a = StudyManager.GetAdminServiceWCFClient();
            a.DeleteSurveyPageCompleted += new EventHandler<DeleteSurveyPageCompletedEventArgs>(delegate(object sender, DeleteSurveyPageCompletedEventArgs e)
            {
                _studyManager.SurveyPages.Remove(SurveyPageToDelete);
                List<SurveySurveyPage> ssp = (from x in _studyManager.SurveySurveyPages
                                              where x.SurveyPageId == SurveyPageToDelete.SurveyPageId
                                              select x).ToList();
                foreach (SurveySurveyPage ssp_todelete in ssp)
                {
                    _studyManager.SurveySurveyPages.Remove(ssp_todelete);
                }
                DeleteSurveyPageCompleted(SurveyPageToDelete);
            });
            a.DeleteSurveyPageAsync(SurveyPageToDelete);
            a.CloseAsync();
        }        
        public delegate void DeleteIndividualStudyResultsCompletedDelegate();
        public event DeleteIndividualStudyResultsCompletedDelegate DeleteIndividualStudyResultsCompleted;
        public void DeleteIndividualStudyResults(Guid StudyId, int IndividualId)
        {
            AdminServiceWCFClient a = StudyManager.GetAdminServiceWCFClient();
            a.DeleteStudyResultsCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(delegate(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
            {
                DeleteIndividualStudyResultsCompleted();
            });
            a.DeleteStudyResultsAsync(StudyId, IndividualId);
        }
        public delegate void DeleteAllStudyDataDelegate();
        public event DeleteAllStudyDataDelegate DeleteAllStudyDataEvent;
        public void DeleteAllStudyData(Guid StudyId)
        {
            AdminServiceWCFClient a = StudyManager.GetAdminServiceWCFClient();
            a.DeleteResultsCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(delegate(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
            {
                DeleteAllStudyDataEvent();
            });
            a.DeleteResultsAsync(StudyId);
        }
        public delegate void RemoveSurveyFromWaveCompletedDelegate(Survey SurveyToRemove);
        public event RemoveSurveyFromWaveCompletedDelegate RemoveSurveyFromWaveCompleted;
        public void RemoveSurveyFromWave(int StudyWaveId, int ConditionId, Survey Survey)
        {
            AdminServiceWCFClient a = StudyManager.GetAdminServiceWCFClient();
            a.RemoveSurveyCompleted += new EventHandler<RemoveSurveyCompletedEventArgs>(delegate(object sender, RemoveSurveyCompletedEventArgs e)
            {
                _studyManager.StudyWaveSurveys.Remove(
                    GetStudyWaveSurveys(StudyWaveId, ConditionId, Survey.SurveyId)
                    );
                RemoveSurveyFromWaveCompleted(Survey);
            });
            a.RemoveSurveyAsync(StudyWaveId, ConditionId, Survey.SurveyId);
            a.CloseAsync();
        }
        #endregion
    }
}
